package vcp.controllers;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import db.parking.beans.rates;
import vcp.application.UserManagement;
import vcp.common.IController;
import vcp.gui.forms.validators.NumericValidator;
import vcp.models.RatesManagementModel;
import vcp.views.RatesApprovalView;
import vcp.views.RatesManagementView;


/**
 * Rates Management Controller
 *
 */
public class RatesManagementController implements
ActionListener,TableModelListener,AncestorListener, IController{
	RatesManagementView view;
	RatesManagementModel model;
	
	RatesApprovalView raview;
    
	
	public RatesManagementController(RatesApprovalView ratesApprovalView) {
		this.raview = ratesApprovalView;
	}
	public void addView(Object view) {
		this.view = (RatesManagementView)view;		
	}
	@Override
	public void addModel(Object model) {
		this.model = (RatesManagementModel)model;
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		CardLayout cl = this.view.getPnlRatesManagement().getCardlayout();
		JPanel spane = this.view.getPnlRatesManagement().getFormPanel();
		switch(e.getActionCommand()){		
		case "NewRate":			
			cl.first(spane);
			break;
		case "EditRate":			
			cl.last(spane);
			break;
		case "SaveRate":
			saveRate();
			break;
		}
		
	}
	private void saveRate() {		
		int lotNo = view.getNewRateForm().getComboLotNum().getSelectedIndex();
		int type = view.getNewRateForm().getComboRateType().getSelectedIndex();
		String amount = view.getNewRateForm().getTextAmount().getText();
		NumericValidator num_v = new NumericValidator();		
		if(lotNo>=0 && type>=0 && num_v.validate(amount)){
			rates bean = new rates();
			bean.setAmount(Integer.parseInt(amount));
			bean.setLotNo(lotNo+1);
			bean.setRateType(type+1);
			this.model.saveRate(bean);
		
		}else{
			 JOptionPane.showMessageDialog(null, "Wrong input...", "Error",
                     JOptionPane.ERROR_MESSAGE);
		}
		

	}
	
	
	@Override
	public void tableChanged(TableModelEvent e) {
		if(e.getType()==TableModelEvent.UPDATE){  //editing
			/* take new value go to database compare if changed do 
			 * update
			 */
			DefaultTableModel tbm = (DefaultTableModel)e.getSource();

			int oldval = Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 3).toString());
			int newval = Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 4).toString());
			if(oldval!=newval){
			  rates bean = new rates();
			  bean.setLotNo(Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 0).toString()));
			  bean.setRateType(Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 1).toString()));
			  bean.setRateName(tbm.getValueAt(e.getFirstRow(), 2).toString());
			  bean.setAmount(Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 3).toString()));
			  bean.setTmpAmount(Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 4).toString()));
			  bean.setStatus(0);
			  model.modifyTempRateCell(bean);
            }
			
			//bean.setLotNo(Integer.parseInt((tbm.getValueAt(e.getFirstRow(), 0).toString())));
			//bean.setRateType(rateType);
			//System.out.println(newValue);
		}
	}
	
	
	
	
	// need for bootstrap
		public RatesManagementView getView() {
			return view;
		}
		
		public RatesManagementModel getModel() {
			return model;
		}
		@Override
		public void ancestorAdded(AncestorEvent arg0) {
			int lotNum = UserManagement.getInstance().getEmployee().getLotNo();
			int rows = view.getPnlRatesManagement().getRatesTable().getDtm().getRowCount();
			if(rows==0){
			  this.model.getRatesByLotNum(lotNum);
			}else{
				view.getPnlRatesManagement().getRatesTable().getDtm().setRowCount(0);
				this.model.getRatesByLotNum(lotNum);
			   // view.getPnlRatesManagement().getRatesTable().repaint();
			}
		}
		@Override
		public void ancestorMoved(AncestorEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void ancestorRemoved(AncestorEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	
	
    
}
